﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using BuildHelper.Work.GUI;

namespace BuildHelper.Work.Short
{
    public partial class ShortGuiControl : XtraUserControl
    {
        public ShortGuiControl()
        {
            InitializeComponent();    
        }
        private bool isLoaded = false;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!this.IsDesignMode())
            {
                layoutProvider1.SaveCurrentLayoutAsDefault();
                isLoaded = true;
                rep_colOpenSln.Buttons[0].Image = commonImageCollectionFull1.Images[28].ResizeImage(18, 18);
                rep_colOpenCsproj.Buttons[0].Image = commonImageCollectionFull1.Images[28].ResizeImage(18, 18);
                rep_colRunExe.Buttons[0].Image = commonImageCollectionFull1.Images[89].ResizeImage(18, 18);
                rep_gridColumn1.Buttons[0].Image = commonImageCollectionFull1.Images[8].ResizeImage(18, 18);
                colRunExe.ColumnEdit = null;

                LoadLayout();
            }
        }
        private BudowanieGui _GuiForm;
        public BudowanieGui GuiForm
        {
            get { return _GuiForm; }
            set
            {
                _GuiForm = value;
                if (value != null)
                {
                    lblBrancz.Text = value.Brancz;
                    if (!value.Pojedyncza)
                        labelControl1.Text += " (zmienny)";
                    value.BranczChanged += new EventHandler(value_BranczChanged);
                    gridControl1.DataSource = value.guiDataSet1;
                }
                else
                {
                    gridControl1.DataSource = null;
                }
            }
        }

        void value_BranczChanged(object sender, EventArgs e)
        {
            lblBrancz.Text = GuiForm.Brancz;
        }

        private void gvStm_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
        {
            if (e.Column == colRunExe)
            {
                DataRow r = gvStm.GetDataRow(e.RowHandle);
                if (r != null && r["EXE"].IsNull())
                    e.RepositoryItem = null;
                else
                    e.RepositoryItem = rep_colRunExe;
            }
        }

        private void modeProvider1_CustomGetModes(object sender, ProgNetComponentsDx.Mode.CustomModeSettingsEventArgs e)
        {
            if (sender == colRunExe && e.Row != null && e.Row["EXE"].IsNull())
                e.IsReadOnly = true;
        }

        private void rep_colOpenCsproj_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            GridView gv = gridControl1.FocusedView as GridView;
            if (gv != null)
            {
                DataRow row = gv.GetFocusedDataRow();
                if (GuiForm != null)
                {
                    GuiForm.OpenCsProj(row);
                }
            }
        }
        private void rep_colOpenSln_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            GridView gv = gridControl1.FocusedView as GridView;
            if (gv != null)
            {
                DataRow row = gv.GetFocusedDataRow();
                if (GuiForm != null)
                {
                    GuiForm.OpenSln(row);
                }
            }
        }
        private void rep_colRunExe_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DataRow r = gvStm.GetFocusedDataRow();
            if (r != null)
            {
                GuiForm.RunExe(r["STM"].AsString());
            }
        }

        private void btnBudujZaznaczone_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.BudujZaznaczone();
        }

        private void btnBudujSTM_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.BudujStm();
        }

        private void btnBudujIUruchom_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.BudujIUruchom();
        }

        private void btnAnuluj_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.AnulujBudowanie();
        }

        private void btnPokazPelnyLog_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.PokazPelnyLog();
        }

        private void btnGuiWczytajTemplate_Click(object sender, EventArgs e)
        {
            if (GuiForm != null)
                GuiForm.WczytajTeplate();
        }

        public void SetCurrentStm(string stm)
        {
            gcSTM.Text = stm;
            btnBudujSTM.Enabled =
            btnBudujIUruchom.Enabled = stm.IsNotNull();
        }

        public void PoczatekBudowania()
        {
            if (progress.InvokeRequired)
            {
                progress.Invoke(new Action(PoczatekBudowania));
                return;
            }
            progress.Properties.Stopped = false;
        }

        public void KoniecBudowania()
        {
            if (progress.InvokeRequired)
            {
                progress.Invoke(new Action(KoniecBudowania));
                return;
            }
            progress.Properties.Stopped = true;
            SetProgressTest("");
        }

        public void SetProgressTest(string text)
        {
            if (progress.InvokeRequired)
            {
                progress.Invoke(new Action<string>(SetProgressTest), text);
                return;
            }
            progress.Text = text;
        }

        private void gvStm_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (GuiForm != null)
            {
                DataRow r = gvStm.GetDataRow(e.FocusedRowHandle);
                GuiForm.FocusRow(r);
            }
        }

        public void SaveLayout()
        {
            if (isLoaded)
                layoutProvider1.SaveLayout();
        }
        public void LoadLayout()
        {
            layoutProvider1.ApplySavedLayout();
        }
        private void rep_gridColumn1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DataRow r = gvStm.GetFocusedDataRow();
            if (r != null)
            {
                string stm = r["STM"].AsString();
                if (GuiForm != null)
                    GuiForm.BudujStm(stm);
            }
        }

        private void gvProjekty_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (GuiForm != null)
            {
                if ((sender as GridView).GetDataRow(e.RowHandle) != null)
                    GuiForm.TemplateName = "";
            }
        }
    }
}
